package com.yyhh.helpcall.activity;

import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.scwang.smart.refresh.footer.BallPulseFooter;
import com.scwang.smart.refresh.header.BezierRadarHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.yyhh.helpcall.R;
import com.yyhh.helpcall.activity.adapter.CallLogsAdapter;
import com.yyhh.helpcall.http.OkHttpUtils;
import com.yyhh.helpcall.http.bean.BaseResponse;
import com.yyhh.helpcall.http.bean.CallLogResponse;
import com.yyhh.helpcall.utils.AppUtils;
import com.yyhh.helpcall.utils.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class CallLogsActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private CallLogsAdapter callLogsAdapter;
    private SmartRefreshLayout smartRefreshLayout;
    private int pageIndex = 1;
    private int pageSize = 20;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_logs);
        initViews();
    }

    private void initViews() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        smartRefreshLayout = findViewById(R.id.refreshLayout);
        BezierRadarHeader bezierRadarHeader = new BezierRadarHeader(this);
        bezierRadarHeader.setAccentColor(getResources().getColor(R.color.colorPrimary));
        smartRefreshLayout.setRefreshHeader(bezierRadarHeader);
        BallPulseFooter ballPulseFooter = new BallPulseFooter(this);
        ballPulseFooter.setAnimatingColor(getResources().getColor(R.color.colorPrimary));
        smartRefreshLayout.setRefreshFooter(ballPulseFooter);
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                requestDatas();
            }
        });
        smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                pageIndex++;
                requestDatas();
            }
        });
        smartRefreshLayout.autoRefresh();
        recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        recyclerView.setLayoutManager(linearLayoutManager);
        callLogsAdapter = new CallLogsAdapter();
        callLogsAdapter.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object obj = v.getTag();
                if (obj != null && obj instanceof CallLogResponse.CallLogDataResponse) {
                    CallLogResponse.CallLogDataResponse data = (CallLogResponse.CallLogDataResponse) obj;
                    AppUtils.callPhone(CallLogsActivity.this, data.customer_tel);
                }
            }
        });
        recyclerView.setAdapter(callLogsAdapter);
    }

    private void requestDatas() {
        HashMap<String, String> map = new HashMap<>();
        map.put("page_idx", this.pageIndex + "");
        map.put("page_num", this.pageSize + "");
        OkHttpUtils.getInstance().getApi().getCallList(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse<CallLogResponse>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse<CallLogResponse> value) {
                        if (value.isSuccess()) {
                            ArrayList<CallLogResponse.CallLogDataResponse> data = value.data.list;
                            if (pageIndex == 1) {
                                callLogsAdapter.datas = data;
                            } else {
                                callLogsAdapter.datas.addAll(data);
                            }
                            if (callLogsAdapter.datas.size() == 0) {
                                ToastUtils.show("暂无相关数据");
                            }
                            callLogsAdapter.notifyDataSetChanged();
                        } else {
                            value.showErrorMessage();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        ToastUtils.show("出现错误,请重试~");
                    }

                    @Override
                    public void onComplete() {
                        smartRefreshLayout.finishRefresh(1000);//传入false表示刷新失败
                        smartRefreshLayout.finishLoadMore(1000);
                    }
                });
    }
}
